package com.platform.api.bzproject.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.dao.BzAllWallpaperMapper;
import com.platform.api.bzproject.model.BzAllWallpaper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/9/25 15:30
 * @className DouyinWallpaperService
 * @description TODO
 * @Version 1.0
 */

@Service
@Transactional(rollbackFor=Exception.class)
public class BzAllWallpaperService extends ServiceImpl<BzAllWallpaperMapper, BzAllWallpaper> {


    @Autowired
    private BzAllWallpaperMapper bzAllWallpaperDao;

    private final String ORDER_BY_ID = "id desc";
    private final String ORDER_BY_BROEWSE_COUNT = "browse_count desc";
    private final String ORDER_BY_LIKE_COUNT = "like_count desc";
    private final String ORDER_BY_COLLECTION_COUNT = "collection_count desc";
    private final String ORDER_BY_DOWN_COUNT = "down_count desc";
    //置顶并且 按照时间
    private final String ORDER_BY_TOP_AND_TIME = "topping desc,wp.id desc";

    /**
     * 设置排序
     * @param order
     * @return
     */
    public String getOrderBy(Integer order){
        if(null == order){
            return ORDER_BY_ID;
        }
        if(0 == order){
            return ORDER_BY_BROEWSE_COUNT;
        }else if(1 == order){
            return ORDER_BY_LIKE_COUNT;
        }else if(2 == order){
            return ORDER_BY_COLLECTION_COUNT;
        }else if(3 == order){
            return ORDER_BY_DOWN_COUNT;
        }else if(4 == order){
            return ORDER_BY_TOP_AND_TIME;
        }
        return ORDER_BY_ID;
    }


    public PageInfo<BzAllWallpaper> selectAllWallpaper(BzAllWallpaper wallpaper, int pageNum, int pageSize, String order){
        PageHelper.startPage(pageNum, pageSize).setOrderBy(order);
        wallpaper.setIsDelete(1);
        final PageInfo<BzAllWallpaper> userPageInfo = new PageInfo<>(bzAllWallpaperDao.selectAllWallpaper(wallpaper));
        return userPageInfo;
    }

    public BzAllWallpaper getWallpaperById(Integer id){
        return bzAllWallpaperDao.getWallpaperById(id);
    }

    /**
     * 获取用户当天已上传的壁纸数量
     * @param openid
     * @return
     */
    public int getUploadCount(String openid){
        return bzAllWallpaperDao.getUploadCount(openid);
    }

    public BzAllWallpaper getLimitOneRight(Map<String,Object> map){
        return bzAllWallpaperDao.getLimitOneRight(map);
    }
    public BzAllWallpaper getLimitOneLeft(Map<String,Object> map){
        return bzAllWallpaperDao.getLimitOneLeft(map);
    }

    public BzAllWallpaper upAndDownData(Map<String,Object> map){
        if(map.get("flag").toString().equals("left")){
            return bzAllWallpaperDao.getLimitOneLeft(map);
        }else if(map.get("flag").toString().equals("right")){
            return bzAllWallpaperDao.getLimitOneRight(map);
        }
        return null;
    }

    public int updateDownCount(BzAllWallpaper wallpaper){
        if(null != wallpaper){
            wallpaper.setDownCount(null != wallpaper.getDownCount() ? wallpaper.getDownCount() + 1 : 1);
            return bzAllWallpaperDao.updateDownCount(wallpaper);
        }
        return 0;
    }

    public List<BzAllWallpaper> getAllUserData(){
        return bzAllWallpaperDao.getAllUserData();
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateAllUserData(List<BzAllWallpaper> list){
        return bzAllWallpaperDao.updateAllUserData(list);
    }

    /**
     * 修改用户所有数据
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateAllUserData(){
        List<BzAllWallpaper> allUserData = bzAllWallpaperDao.getAllUserData();
        int i = updateAllUserData(allUserData);
        return i;
    }

    public List<Integer> getUserClassIds(String userId){
        return bzAllWallpaperDao.getUserClassIds(userId);
    }
}
